package cn.smart.epic.modular.system.service.impl;

import cn.smart.epic.common.constant.Const;
import cn.smart.epic.common.persistence.dao.PermissionMapper;
import cn.smart.epic.modular.system.service.PermissionService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Auther Uncle_Z
 * @date 2018/7/24 14:09
 * @Email:2545375@qq.com
 * @Descrition
 * @since 1.0
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    private Logger logger = LoggerFactory.getLogger(PermissionServiceImpl.class);

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public JSONObject getUserPermission(String username,List<Integer> roleIds) {
        JSONObject userPermission = getUserPermissionFromDB(username,roleIds);
        return userPermission;
    }

    private JSONObject getUserPermissionFromDB(String username,List<Integer> roleIds){
        JSONObject userPermission = permissionMapper.getUserPermission(username,roleIds);
        String roleIdKey = "roleId";
        if (userPermission.getIntValue(roleIdKey)==Const.ADMIN_ROLE_ID) {
            Set<String> menuList = permissionMapper.getAllMenu();
            Set<String> permissionList = permissionMapper.getAllPermission();
            userPermission.put("menuList", menuList);
            userPermission.put("permissionList", permissionList);
        }
        return userPermission;
    }
}
